package xyz.yuzegod.megawalls.classes.normal.shaman;

import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.*;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.util.ItemBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Shaman extends Classes
{
    public static Map<GamePlayer, Integer> skill3;
    
    public Shaman() {
        super("Shaman", "萨满", ChatColor.GREEN, Material.ENCHANTMENT_TABLE, (byte)0, ClassesType.NORMAL, new ClassesInfo.Orientation[] { ClassesInfo.Orientation.WARRIOR, ClassesInfo.Orientation.CONTROL }, ClassesInfo.Difficulty.ONE);
        this.setMainSkill(new MainSkill(this));
        this.setSecondSkill(new SecondSkill(this));
        this.setThirdSkill(new ThirdSkill(this));
        this.setCollectSkill(new FourthSkill(this));
        this.setDefaultSkin("eyJ0aW1lc3RhbXAiOjE1NDg4MTY3NjQ3NTQsInByb2ZpbGVJZCI6IjkxOGEwMjk1NTlkZDRjZTZiMTZmN2E1ZDUzZWZiNDEyIiwicHJvZmlsZU5hbWUiOiJCZWV2ZWxvcGVyIiwic2lnbmF0dXJlUmVxdWlyZWQiOnRydWUsInRleHR1cmVzIjp7IlNLSU4iOnsidXJsIjoiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS8zMmE2M2I1ZjhlZGMwMGU3ODNmM2Y4YzhkMzBhYmQ1MDg3OTYwMTkzMzczMmRhMmE2MTcyYTZmYTFkMzVlNWI2In19fQ==", "oNNUuGnm0T0j6l8CkSBswIdsziUhHx0u6ALPnYfg8BxEtoaIUwh8MJL0EDYtrap9nW8qTE/tyuT423jAOJ/ZAbVYod//bE8aBLEtA0Q5Vdxi+pAd4NvHQunG9ce0WfMAZaseCemk2rI/t2+ijTNjAIL6T7fpTWOvmcNd6XyO5NIkH3YhPla1fCCvotHPVemPk6bPWk4eCMphhl7/rpNNR2HZB2HWAGORZfgBa2CSSJiFKTfi4rc5Ro82QLy5vqFzdUVDtDGFZLtrvM6u27ZuAgYSQ7l7c2d9gnSAVpwRFHbYTWVXkN+TQ8CntUJnw6m1l0V890qWQQMNmZwERduI41+0m2T2dmGK+2VCnF0N5T59HYQe5opUSL6U41iHe4vMPIEf/pdBrhKh/JqhEopBp1+KxN3EMh27hcivayuFHpPWjzY9SuXeHO5/y3kqIp/jb1ptAOjUEXpHuVjm40p8PmsI0YjKKS/Hx7bz6C003k798D8qCVXLCfr/Dt47Z0i0BLsMAeyetjO/1ndVdf7a1/Rv4L2Nql3hriV8LZmc9ZSsKlHV4RIYr5ebZaWwbHii/CEFu7NvmMXsL0nI4dkEpGjDeFURG7U8d2AAiirndWn+23IZmeF+2GqNE5aeVbq9dZjDjIsta3ysBU4wHkARAWhp8b88i5e5of3VjEGQS4E=");
        this.setEquipmentPackage(new EquipmentPackage(this) {
            @Override
            public List<ItemStack> getEquipments(final int level) {
                final List<ItemStack> items = new ArrayList<ItemStack>();
                items.add(new ItemBuilder(Material.COMPASS).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 指南针").build());
                items.add(new ItemBuilder(Material.DIAMOND_PICKAXE).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 镐").setUnbreakable(true).addEnchantment(Enchantment.DIG_SPEED, 3).addEnchantment(Enchantment.DURABILITY, 3).build());
                items.add(new ItemBuilder(Material.ENDER_CHEST).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 末影箱").build());
                switch (level) {
                    case 1: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        break;
                    }
                    case 2: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_FALL, 1).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 3: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).addEnchantment(Enchantment.PROTECTION_FALL, 1).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 4: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.DIAMOND_BOOTS).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).addEnchantment(Enchantment.PROTECTION_FALL, 2).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 5: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.DIAMOND_BOOTS).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2).addEnchantment(Enchantment.PROTECTION_FALL, 2).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Shaman.this.nameColor + Shaman.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
    }
                }
                if (xyz.yuzegod.megawalls.util.ItemUtils.containsBow(items)) {
                    return items;
                }else {
                    items.add(new ItemBuilder(Material.BOW).setDisplayName(nameColor+getDisplayName()+" 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY,10).build());
                    return items;
                }
            }
            
            @Override
            public List<String> getInfo(final int level) {
                final List<String> lore = new ArrayList<String>();
                switch (level) {
                    case 1: {
                        lore.add(" §8▪ §7铁 剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7治疗药水 8❤");
                        lore.add(" §8▪ §7牛排");
                        break;
                    }
                    case 2: {
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §a+ 摔落保护 I");
                        lore.add(" §a+ §7速度药水 II");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7牛排 §8x1 ➜ §ax2");
                        break;
                    }
                    case 3: {
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §a+ 保护 I");
                        lore.add("    §8▪ 摔落保护 I");
                        lore.add(" §8▪ §7速度药水 II §8x1 ➜ §ax2");
                        lore.add("    §8▪ 0:15");
                        break;
                    }
                    case 4: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I");
                        lore.add("    §8▪ 摔落保护 I ➜ §aII");
                        lore.add(" §8▪ §7治疗药水 8❤ §8x1 ➜ §ax2");
                        lore.add(" §8▪ §7牛排 §8x2 ➜ §ax3");
                        break;
                    }
                    case 5: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7钻石 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I ➜ §aII");
                        lore.add("    §8▪ 摔落保护 II");
                        break;
                    }
                }
                return lore;
            }
        });
    }
    
    @Override
    public List<String> getInfo() {
        final List<String> lore = new ArrayList<String>();
        lore.add("§7你热爱妨碍敌人.");
        lore.add("§7你想拥有一只小狗.");
        lore.add("§7你讨厌挖煤.");
        return lore;
    }
    
    @Override
    public int unlockCost() {
        return 5000;
    }
    
    @Override
    public int energyMelee() {
        return 10;
    }
    
    @Override
    public int energyBow() {
        return 10;
    }
    
    @Override
    public void run() {
        GamePlayer.getOnlinePlayers().stream().filter(gamePlayer -> !gamePlayer.isSpectator() && ClassesManager.getSelected(gamePlayer).equals(this)).forEach(gamePlayer -> gamePlayer.sendActionBar(this.getSkillTip(gamePlayer)));
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.mainSkill.getSkillTip(gamePlayer);
    }
    
    @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
    public void onEntityDamageByEntity(final EntityDamageByEntityEvent e) {
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) {
            return;
        }
        if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
            final GamePlayer gamePlayer = GamePlayer.get(e.getEntity().getUniqueId());
            if (gamePlayer.isSpectator() || gamePlayer.getGameTeam().isInTeam(GamePlayer.get(e.getDamager().getUniqueId()))) {
                return;
            }
            Classes classes = ClassesManager.getSelected(gamePlayer);
            if (classes.equals(this)) {
                this.thirdSkill.use(gamePlayer, gamePlayer.getPlayerStats().getKitStats(classes));
            }
            final GamePlayer damagerPlayer = GamePlayer.get(e.getDamager().getUniqueId());
            if (gamePlayer.isSpectator() || gamePlayer.getGameTeam().isInTeam(GamePlayer.get(e.getDamager().getUniqueId()))) {
                return;
            }
            classes = ClassesManager.getSelected(damagerPlayer);
            if (classes.equals(this) && MegaWalls.getRandom().nextInt(100) <= 17) {
                final int seconds = ((SecondSkill)this.secondSkill).getSeconds(damagerPlayer.getPlayerStats().getKitStats(classes).getSkill2Level());
                final Player damager = (Player)e.getDamager();
                if (damager.hasPotionEffect(PotionEffectType.SPEED)) {
                    damager.removePotionEffect(PotionEffectType.SPEED);
                }
                damager.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, seconds * 20, 1));
                final Player entity = (Player)e.getEntity();
                if (entity.hasPotionEffect(PotionEffectType.WEAKNESS)) {
                    entity.removePotionEffect(PotionEffectType.WEAKNESS);
                }
                entity.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, seconds * 20, 0));
            }
        }
    }
    
    static {
        Shaman.skill3 = new HashMap<GamePlayer, Integer>();
    }
}
